import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;


public class Hostess{
	public static int EmpID;
	static Integer[] blueSection={31, 41, 42};
	static Integer redSection[]={11,21,12};
	static Integer yellowSection[]={22,32,23,33};
	static Integer whiteSection[]={13,14,24};
	static Integer geySection[]={43,34,44};
	private static ArrayList<String> resHeaders;

	public static void hostess(int empID){
		GUI.window.setTitle("Hostess Screen");
		EmpID=empID;
		GUI.bottomPanel=GUI.exitOnly();
		//state="serverMain"; //Will be changed
		hostessMain();

		GUI.windowDone();

	}
	static void hostessMain() {
		GUI.resetPanels();
		GUI.leftMenu.add(Manager.reservationsButton(EmpID));
		GUI.leftMenu.add(waitListButton(EmpID));
		GUI.leftMenu.add(GUI.takeoutButton(40, EmpID));

		for(int i=1;i<5;i++){ // make editable
			for(int j=1;j<5;j++){
				Integer table=j*10+i;
				if(Server.contains(table,blueSection))
					GUI.centerPanel.add(Hostess.newTable(table, 75, GUI.blue, Color.black));
				else
					if(Server.contains(table, redSection))
						GUI.centerPanel.add(Hostess.newTable(table, 75, Color.RED, Color.black));
					else if(Server.contains(table, yellowSection))
						GUI.centerPanel.add(Hostess.newTable(table, 75, GUI.yellow, Color.black));
					else if(Server.contains(table, whiteSection))
						GUI.centerPanel.add(Hostess.newTable(table, 75, Color.WHITE, Color.black));
					else
						GUI.centerPanel.add(Hostess.newTable(table, 75, Color.GRAY, Color.black));

			}
		}
		GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
		GUI.bottomPanel.add(GUI.exitOnly());
	}

	public static JButton newTable(Integer tableNum, int size, Color color, Color text){
		JButton button=GUI.newButton(Integer.toString(tableNum), 40);
		button.setBackground(color);
		button.setForeground(text);
		Border thickBorder=new LineBorder(GUI.teal, size);
		button.setBorder(thickBorder);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				button.setText(HostessAtTable(tableNum, EmpID));
			}

		});
		return button;
	}

	protected static String HostessAtTable(Integer tableNum, int empID2) {
		String selection=PopUp.hostessTable(tableNum);
		return selection;

	}
	static void waitListPanel(int empID2) {
		resHeaders=new ArrayList<String>();
		resHeaders.add("Date");
		resHeaders.add("Time");
		resHeaders.add("# In Party");
		resHeaders.add("Name");
		resHeaders.add("Phone Number");
		resHeaders.add("Notes");

		//test for data array (blank lines)
		ArrayList<String> blankTest = new ArrayList<String>();
		//		for (int i = 0; i < 51; i++){
		//			blankTest.add("");
		//		}

		blankTest.add("11/05/2015");
		blankTest.add("09:30");
		blankTest.add("4");
		blankTest.add("Jones");
		blankTest.add("999-999-9999");
		blankTest.add("");
		blankTest.add("12/05/2015");
		blankTest.add("07:30");
		blankTest.add("6");
		blankTest.add("Carter");
		blankTest.add("999-999-9999");
		blankTest.add("Test");

		GUI.window.setTitle("WaitList List");
		GUI.centerPanel.setLayout(new BorderLayout());
		GUI.centerPanel.add(new Grids(resHeaders.size(), resHeaders, blankTest.size()/resHeaders.size(), blankTest));         ////// REPLACE LINE 109 WITH THIS GRID /////
		//GUI.centerPanel.add(new Label("Table "+tableNum.toString()));
		GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
		GUI.leftMenu.add(addWaitList(EmpID));
		GUI.leftMenu.add(editWaitList(EmpID));
		GUI.leftMenu.add(removeWaitList(EmpID));

		GUI.leftPanel.removeAll();
		GUI.leftPanel.add(GUI.leftMenu);
		GUI.bottomPanel.add(GUI.mainMenuBar(empID2));
	}

	static JButton waitListButton(int empID2){
		JButton button=GUI.newButton("Wait List", 40);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUI.resetPanels();
				waitListPanel(empID2);
			}
		});
		return button;
	}

	private static Component removeWaitList(int empID2) {
		JButton button=GUI.newButton("Remove Wait List", 40);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//GUI.resetPanels();
				Grids.deleteResult(resHeaders);
			}
		});
		return button;
	}
	private static Component editWaitList(int empID2) {
		JButton button=GUI.newButton("Edit Wait List", 40);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//GUI.resetPanels();
				//Obtain the results from the highlighted row
				ArrayList<String> results = Grids.getEnteredResults();
				if(results != null){

					ArrayList<String> modResults = PopUp.runReservation(results); 
					//if a parsing error is found pass the arraylist back to runReservation
					while(modResults != null && modResults.get(modResults.size()-1).equals("ERROR")){
						modResults = PopUp.runReservation(modResults);
					}
					if (modResults != null){
						Grids.modifyResult(modResults);
					}
				}
			}
		});
		return button;
	}

	private static Component addWaitList(int empID2) {
		JButton button=GUI.newButton("Add Wait List", 40);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//GUI.resetPanels();
				//A blank arraylist is used if a parsing error is found so previous data
				//entered is not lost
				ArrayList<String> blank = new ArrayList<String>();
				for (int i = 0; i < resHeaders.size(); i++){
					blank.add(null);
				}
				ArrayList<String> results = PopUp.runReservation(blank);
				//if a parsing error is found pass the arraylist back to runReservation
				while(results != null && results.get(results.size()-1).equals("ERROR")){
					results = PopUp.runReservation(results);
				}
				if(results != null){
					Grids.addNewResult(results, resHeaders);
				}
			}
		});
		return button;
	}

}
